import 'package:flutter/material.dart';
import 'package:jiyun_behavior/screens/welcome_screen.dart';
import 'package:jiyun_behavior/utils/app_theme.dart';
import 'package:jiyun_behavior/widgets/custom_button.dart';

class ProfileScreen extends StatefulWidget {
  const ProfileScreen({Key? key}) : super(key: key);

  @override
  State<ProfileScreen> createState() => _ProfileScreenState();
}

class _ProfileScreenState extends State<ProfileScreen> {
  bool _notificationsEnabled = true;
  bool _darkModeEnabled = false;
  String _selectedFontSize = '中';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('个人中心'),
      ),
      body: SafeArea(
        child: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              children: [
                _buildProfileHeader(),
                const SizedBox(height: 20),
                _buildAccountInfo(),
                const SizedBox(height: 20),
                _buildAstrologyInfo(),
                const SizedBox(height: 20),
                _buildAppSettings(),
                const SizedBox(height: 20),
                _buildAboutUs(),
                const SizedBox(height: 30),
                _buildLogoutButton(),
                const SizedBox(height: 20),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildProfileHeader() {
    return Center(
      child: Column(
        children: [
          Stack(
            children: [
              Container(
                width: 80,
                height: 80,
                decoration: BoxDecoration(
                  color: AppTheme.secondaryColor,
                  shape: BoxShape.circle,
                ),
                child: const Center(
                  child: Icon(
                    Icons.person,
                    size: 36,
                    color: AppTheme.primaryColor,
                  ),
                ),
              ),
              Positioned(
                bottom: 0,
                right: 0,
                child: Container(
                  width: 24,
                  height: 24,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    shape: BoxShape.circle,
                    boxShadow: [
                      BoxShadow(
                        color: Colors.black.withOpacity(0.1),
                        blurRadius: 2,
                        offset: const Offset(0, 1),
                      ),
                    ],
                  ),
                  child: const Center(
                    child: Icon(
                      Icons.camera_alt,
                      size: 12,
                      color: AppTheme.subtitleColor,
                    ),
                  ),
                ),
              ),
            ],
          ),
          const SizedBox(height: 16),
          const Text(
            '张先生',
            style: TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 4),
          Text(
            '黄金会员',
            style: TextStyle(
              fontSize: 14,
              color: AppTheme.subtitleColor,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildAccountInfo() {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '账号信息',
              style: Theme.of(context).textTheme.titleLarge,
            ),
            const SizedBox(height: 16),
            _buildSettingItem(
              title: '手机号码',
              trailing: Row(
                children: [
                  Text(
                    '138****6666',
                    style: TextStyle(
                      fontSize: 14,
                      color: AppTheme.lightTextColor,
                    ),
                  ),
                  const SizedBox(width: 8),
                  Icon(
                    Icons.chevron_right,
                    size: 16,
                    color: AppTheme.lightTextColor,
                  ),
                ],
              ),
              onTap: () {
                // 修改手机号码逻辑
              },
            ),
            const Divider(),
            _buildSettingItem(
              title: '修改密码',
              trailing: Icon(
                Icons.chevron_right,
                size: 16,
                color: AppTheme.lightTextColor,
              ),
              onTap: () {
                // 修改密码逻辑
              },
            ),
            const Divider(),
            _buildSettingItem(
              title: '实名认证',
              trailing: Row(
                children: [
                  Text(
                    '已认证',
                    style: TextStyle(
                      fontSize: 14,
                      color: const Color(0xFF52C41A),
                    ),
                  ),
                  const SizedBox(width: 8),
                  Icon(
                    Icons.chevron_right,
                    size: 16,
                    color: AppTheme.lightTextColor,
                  ),
                ],
              ),
              onTap: () {
                // 实名认证逻辑
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildAstrologyInfo() {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '命理信息',
              style: Theme.of(context).textTheme.titleLarge,
            ),
            const SizedBox(height: 16),
            _buildSettingItem(
              title: '生辰八字',
              trailing: Row(
                children: [
                  Text(
                    '癸亥年 癸卯月 甲辰日 辰时',
                    style: TextStyle(
                      fontSize: 14,
                      color: AppTheme.lightTextColor,
                    ),
                  ),
                  const SizedBox(width: 8),
                  Icon(
                    Icons.chevron_right,
                    size: 16,
                    color: AppTheme.lightTextColor,
                  ),
                ],
              ),
              onTap: () {
                // 查看生辰八字逻辑
              },
            ),
            const Divider(),
            _buildSettingItem(
              title: '命理基本盘',
              trailing: Icon(
                Icons.chevron_right,
                size: 16,
                color: AppTheme.lightTextColor,
              ),
              onTap: () {
                // 查看命理基本盘逻辑
              },
            ),
            const Divider(),
            _buildSettingItem(
              title: '运势报告',
              trailing: Row(
                children: [
                  Text(
                    '5份',
                    style: TextStyle(
                      fontSize: 14,
                      color: AppTheme.primaryColor,
                    ),
                  ),
                  const SizedBox(width: 8),
                  Icon(
                    Icons.chevron_right,
                    size: 16,
                    color: AppTheme.lightTextColor,
                  ),
                ],
              ),
              onTap: () {
                // 查看运势报告逻辑
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildAppSettings() {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '应用设置',
              style: Theme.of(context).textTheme.titleLarge,
            ),
            const SizedBox(height: 16),
            _buildSettingItem(
              title: '消息通知',
              trailing: Switch(
                value: _notificationsEnabled,
                activeColor: AppTheme.primaryColor,
                onChanged: (value) {
                  setState(() {
                    _notificationsEnabled = value;
                  });
                },
              ),
              onTap: () {
                setState(() {
                  _notificationsEnabled = !_notificationsEnabled;
                });
              },
            ),
            const Divider(),
            _buildSettingItem(
              title: '深色模式',
              trailing: Switch(
                value: _darkModeEnabled,
                activeColor: AppTheme.primaryColor,
                onChanged: (value) {
                  setState(() {
                    _darkModeEnabled = value;
                  });
                  // 切换深色模式逻辑
                },
              ),
              onTap: () {
                setState(() {
                  _darkModeEnabled = !_darkModeEnabled;
                });
                // 切换深色模式逻辑
              },
            ),
            const Divider(),
            _buildSettingItem(
              title: '字体大小',
              trailing: Row(
                children: [
                  Text(
                    _selectedFontSize,
                    style: TextStyle(
                      fontSize: 14,
                      color: AppTheme.lightTextColor,
                    ),
                  ),
                  const SizedBox(width: 8),
                  Icon(
                    Icons.chevron_right,
                    size: 16,
                    color: AppTheme.lightTextColor,
                  ),
                ],
              ),
              onTap: () {
                _showFontSizeDialog();
              },
            ),
            const Divider(),
            _buildSettingItem(
              title: '清除缓存',
              trailing: Row(
                children: [
                  Text(
                    '23.5MB',
                    style: TextStyle(
                      fontSize: 14,
                      color: AppTheme.lightTextColor,
                    ),
                  ),
                  const SizedBox(width: 8),
                  Icon(
                    Icons.chevron_right,
                    size: 16,
                    color: AppTheme.lightTextColor,
                  ),
                ],
              ),
              onTap: () {
                _showClearCacheDialog();
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildAboutUs() {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '关于我们',
              style: Theme.of(context).textTheme.titleLarge,
            ),
            const SizedBox(height: 16),
            _buildSettingItem(
              title: '当前版本',
              trailing: Text(
                'v1.0.0',
                style: TextStyle(
                  fontSize: 14,
                  color: AppTheme.lightTextColor,
                ),
              ),
              onTap: () {
                // 检查更新逻辑
              },
            ),
            const Divider(),
            _buildSettingItem(
              title: '用户协议',
              trailing: Icon(
                Icons.chevron_right,
                size: 16,
                color: AppTheme.lightTextColor,
              ),
              onTap: () {
                // 查看用户协议逻辑
              },
            ),
            const Divider(),
            _buildSettingItem(
              title: '隐私政策',
              trailing: Icon(
                Icons.chevron_right,
                size: 16,
                color: AppTheme.lightTextColor,
              ),
              onTap: () {
                // 查看隐私政策逻辑
              },
            ),
            const Divider(),
            _buildSettingItem(
              title: '联系客服',
              trailing: Icon(
                Icons.chevron_right,
                size: 16,
                color: AppTheme.lightTextColor,
              ),
              onTap: () {
                // 联系客服逻辑
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildLogoutButton() {
    return Center(
      child: CustomButton(
        text: '退出登录',
        onPressed: () {
          _showLogoutDialog();
        },
        width: 240,
        isFullWidth: false,
        backgroundColor: const Color(0xFFF5F5F5),
        textColor: AppTheme.subtitleColor,
      ),
    );
  }

  Widget _buildSettingItem({
    required String title,
    required Widget trailing,
    required VoidCallback onTap,
  }) {
    return InkWell(
      onTap: onTap,
      child: Padding(
        padding: const EdgeInsets.symmetric(vertical: 12.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              title,
              style: const TextStyle(
                fontSize: 16,
              ),
            ),
            trailing,
          ],
        ),
      ),
    );
  }

  void _showFontSizeDialog() {
    showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: const Text('选择字体大小'),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              _buildFontSizeOption('小'),
              _buildFontSizeOption('中'),
              _buildFontSizeOption('大'),
              _buildFontSizeOption('特大'),
            ],
          ),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text('取消'),
            ),
          ],
        );
      },
    );
  }

  Widget _buildFontSizeOption(String size) {
    return InkWell(
      onTap: () {
        setState(() {
          _selectedFontSize = size;
        });
        Navigator.of(context).pop();
      },
      child: Padding(
        padding: const EdgeInsets.symmetric(vertical: 12.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              size,
              style: TextStyle(
                fontSize: size == '小' ? 14 : (size == '中' ? 16 : (size == '大' ? 18 : 20)),
              ),
            ),
            if (_selectedFontSize == size)
              const Icon(
                Icons.check,
                color: AppTheme.primaryColor,
              ),
          ],
        ),
      ),
    );
  }

  void _showClearCacheDialog() {
    showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: const Text('清除缓存'),
          content: const Text('确定要清除应用缓存吗？这将不会删除您的个人数据。'),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text('取消'),
            ),
            TextButton(
              onPressed: () {
                // 清除缓存逻辑
                Navigator.of(context).pop();
                ScaffoldMessenger.of(context).showSnackBar(
                  const SnackBar(
                    content: Text('缓存已清除'),
                    backgroundColor: AppTheme.primaryColor,
                  ),
                );
              },
              child: const Text('确定'),
            ),
          ],
        );
      },
    );
  }

  void _showLogoutDialog() {
    showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: const Text('退出登录'),
          content: const Text('确定要退出登录吗？'),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text('取消'),
            ),
            TextButton(
              onPressed: () {
                // 退出登录逻辑
                Navigator.of(context).pop();
                Navigator.of(context).pushAndRemoveUntil(
                  MaterialPageRoute(builder: (context) => const WelcomeScreen()),
                  (route) => false,
                );
              },
              child: const Text('确定'),
            ),
          ],
        );
      },
    );
  }
} 